/* 首页路由模块 */
const express = require('express');
const home = express.Router();

home.get('/article', require('./home/article/list'))
home.get('/article/:id', require('./home/article/info'))
home.get('/mood', require('./home/mood/list'))
home.get('/category', require('./home/category/list'))
home.get('/collection/category', require('./home/collection/category-list'))
home.get('/collection', require('./home/collection/list'))
home.get('/banner', require('./home/banner/list'))

home.post('/login', require('./home/user/login'))
home.post('/register', require('./home/user/register'))

home.get('/guestbook', require('./home/guestbook/list'))
home.post('/guestbook', require('./home/guestbook/add'))
home.post('/comment', require('./home/comment/add'))
home.put('/comment/like', require('./home/comment/like'))


// 上传图片
const multer = require('multer')
const path = require("path")

var storage = multer.diskStorage({
    // 配置文件上传后存储的路径
    destination: function(req, file, cb) {
        cb(null, path.join(__dirname, '../public/upload'))
    },
    // 配置文件上传后存储的路径和文件名
    filename: function(req, file, cb) {
        // cb(null, Date.now() + path.extname(file.originalname))
        cb(null, Date.now() + file.originalname)
    }
})
var upload = multer({
    storage: storage
})
home.post('/upload', upload.single('wangeditor-uploaded-image'), function(req, res, next) {
    const paths = req.file.path.split('\\')
    const len = paths.length
    res.json({
        "errno": 0, // 注意：值是数字，不能是字符串
        "data": {
            "url": '/' + paths[len - 2] + '/' + paths[len - 1], // 图片 src ，必须
        }
    })
})

module.exports = home;